package com.gewu.pm.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.LogUtils;
import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.ui.activity.filter.FilterClockInRecordActivity;
import com.gewu.pm.ui.activity.filter.FilterClockInRecordSubordinateActivity;
import com.gewu.pm.ui.activity.filter.FilterDistributorActivity;
import com.gewu.pm.ui.activity.filter.FilterFreezerManagementActivity;
import com.gewu.pm.ui.activity.filter.FilterFreezerStatisticsActivity;
import com.gewu.pm.ui.activity.filter.FilterGoldBeanManagementActivity;
import com.gewu.pm.ui.activity.filter.FilterMyApplicationActivity;
import com.gewu.pm.ui.activity.filter.FilterMyApprovalActivity;
import com.gewu.pm.ui.activity.filter.FilterPaymentActivity;
import com.gewu.pm.ui.activity.filter.FilterPromotionVisitActivity;
import com.gewu.pm.ui.activity.filter.FilterMultipleActivity;
import com.gewu.pm.ui.activity.filter.FilterSalesLeadActivity;
import com.gewu.pm.ui.activity.filter.FilterSalesOrderActivity;
import com.gewu.pm.ui.activity.filter.FilterSalespersonActivity;
import com.gewu.pm.ui.activity.filter.FilterTerminalManagementActivity;
import com.gewu.pm.ui.activity.filter.FilterTerminalSalesAnalysisActivity;
import com.gewu.pm.ui.activity.filter.FilterVisitManagementActivity;
import com.gewu.pm.ui.activity.filter.FilterWireTransferActivity;
import com.gewu.pm.ui.activity.filter.bean.FilterResultBean;
import com.gewu.pm.utils.eventbus.EventBean;
import com.gewu.pm.utils.eventbus.EventCode;

import java.util.Map;

/**
 * 筛选框架测试页面
 * <p>
 * 功能：
 * - 测试所有新创建的筛选Activity
 * - 点击按钮跳转到对应的筛选页面
 * - 通过EventBus接收筛选结果并打印日志
 * <p>
 * 使用EventBus的优势：
 * - 不需要使用startActivityForResult和onActivityResult
 * - 代码更简洁，解耦性更好
 * - 符合项目现有的EventBus使用规范
 *
 * @author AI Assistant
 * @since 1.0
 */
public class TestFilterActivity extends AppActivity {

    private static final String TAG = "TestFilterActivity";

    // 按钮
    private Button btnSalesperson;
    private Button btnPromotionVisit;
    private Button btnVisitManagement;
    private Button btnFreezerManagement;
    private Button btnFreezerStatistics;
    private Button btnMyApproval;
    private Button btnMyApplication;
    private Button btnClockInRecord;
    private Button btnClockInRecord2;
    private Button btnTerminalManagement;
    private Button btnTerminalSales;
    private Button btnDistributor;
    private Button btnGoldBean;
    private Button btnSalesLead;
    private Button btnPayment;
    private Button btnWireTransfer;
    private Button btnSalesOrder;
    private Button btnProxyPayment;

    /**
     * 启动方法
     */
    public static void start(Context context) {
        Intent intent = new Intent(context, TestFilterActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_test_filter;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        // 初始化所有按钮
        btnSalesperson = findViewById(R.id.btn_salesperson);
        btnPromotionVisit = findViewById(R.id.btn_promotion_visit);
        btnVisitManagement = findViewById(R.id.btn_visit_management);
        btnFreezerManagement = findViewById(R.id.btn_freezer_management);
        btnFreezerStatistics = findViewById(R.id.btn_freezer_statistics);
        btnMyApproval = findViewById(R.id.btn_my_approval);
        btnMyApplication = findViewById(R.id.btn_my_application);
        btnClockInRecord = findViewById(R.id.btn_clock_in_record);
        btnClockInRecord2 = findViewById(R.id.btn_clock_in_record2);
        btnTerminalManagement = findViewById(R.id.btn_terminal_management);
        btnTerminalSales = findViewById(R.id.btn_terminal_sales);
        btnDistributor = findViewById(R.id.btn_distributor);
        btnGoldBean = findViewById(R.id.btn_gold_bean);
        btnSalesLead = findViewById(R.id.btn_sales_lead);
        btnPayment = findViewById(R.id.btn_payment);
        btnWireTransfer = findViewById(R.id.btn_wire_transfer);
        btnSalesOrder = findViewById(R.id.btn_sales_order);
        btnProxyPayment = findViewById(R.id.btn_proxy_payment);

        // 设置点击监听
        setOnClickListener(
                btnSalesperson,
                btnPromotionVisit,
                btnVisitManagement,
                btnFreezerManagement,
                btnFreezerStatistics,
                btnMyApproval,
                btnMyApplication,
                btnClockInRecord,
                btnTerminalManagement,
                btnTerminalSales,
                btnDistributor,
                btnGoldBean,
                btnSalesLead,
                btnPayment,
                btnWireTransfer,
                btnSalesOrder,
                btnProxyPayment,
                btnClockInRecord2
        );
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_proxy_payment) {
            // 代支付筛选/拜访管理-终端/订单汇总报表/未下单客户报表/终端应收报表/配送管理(订单)/配送管理(终端)
            FilterMultipleActivity.start(this);
        } else if (id == R.id.btn_salesperson) {
            // 业务员筛选
            FilterSalespersonActivity.start(this);

        } else if (id == R.id.btn_promotion_visit) {
            // 促销拜访筛选
            FilterPromotionVisitActivity.start(this);

        } else if (id == R.id.btn_visit_management) {
            // 拜访管理筛选
            FilterVisitManagementActivity.start(this);

        } else if (id == R.id.btn_freezer_management) {
            // 冰柜管理筛选
            FilterFreezerManagementActivity.start(this);

        } else if (id == R.id.btn_freezer_statistics) {
            // 冰柜统计筛选
            FilterFreezerStatisticsActivity.start(this);

        } else if (id == R.id.btn_my_approval) {
            // 我的审批筛选
            FilterMyApprovalActivity.start(this);

        } else if (id == R.id.btn_my_application) {
            // 我的申请筛选
            FilterMyApplicationActivity.start(this);

        } else if (id == R.id.btn_clock_in_record) {
            // 打卡记录筛选
            FilterClockInRecordActivity.start(this);

        } else if (id == R.id.btn_clock_in_record2) {
            // 打卡记录筛选-下属
            FilterClockInRecordSubordinateActivity.start(this);

        } else if (id == R.id.btn_terminal_management) {
            // 终端管理筛选
            FilterTerminalManagementActivity.start(this);

        } else if (id == R.id.btn_terminal_sales) {
            // 终端销售分析筛选
            FilterTerminalSalesAnalysisActivity.start(this);

        } else if (id == R.id.btn_distributor) {
            // 配送商筛选
            FilterDistributorActivity.start(this);

        } else if (id == R.id.btn_gold_bean) {
            // 金豆管理筛选
            FilterGoldBeanManagementActivity.start(this);

        } else if (id == R.id.btn_sales_lead) {
            // 销售线索筛选
            FilterSalesLeadActivity.start(this);

        } else if (id == R.id.btn_payment) {
            // 跳转到收款单筛选
            FilterPaymentActivity.start(this);
        } else if (id == R.id.btn_wire_transfer) {
            // 跳转到电汇收款筛选
            FilterWireTransferActivity.start(this);
        } else if (id == R.id.btn_sales_order) {
            // 跳转到销售订单筛选
            FilterSalesOrderActivity.start(this);
        }
    }

    @Override
    protected void initData() {
        // 无需初始化数据
    }

    /**
     * 是否注册EventBus
     * <p>
     * AppActivity已经封装好了EventBus的注册和注销
     * 只需要重写此方法返回true即可
     */
    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    /**
     * 接收EventBus事件
     * <p>
     * 重写AppActivity的receiveEvent方法
     * 不需要使用@Subscribe注解，AppActivity已经处理了
     *
     * @param event EventBus事件
     */
    @Override
    protected void receiveEvent(EventBean event) {
        super.receiveEvent(event);

        // 判断是否是筛选结果事件
        if (event.getCode() == EventCode.FILTER_RESULT) {
            // 获取筛选结果数据
            FilterResultBean resultBean = (FilterResultBean) event.getData();
            if (resultBean == null) {
                return;
            }

            // 获取页面名称和筛选数据
            String pageName = resultBean.getPageName();
            Map<String, Object> data = resultBean.getData();

            // 打印分隔线
            LogUtils.d(TAG, "========================================");
            LogUtils.d(TAG, "接收到筛选结果: " + getPageDisplayName(pageName));
            LogUtils.d(TAG, "========================================");

            // 打印所有筛选数据
            if (data == null || data.isEmpty()) {
                LogUtils.d(TAG, "未选择任何筛选条件");
            } else {
                for (Map.Entry<String, Object> entry : data.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();

                    // 格式化输出
                    String valueStr = value != null ? value.toString() : "null";
                    LogUtils.d(TAG, String.format("%-20s = %s", key, valueStr));
                }
            }

            LogUtils.d(TAG, "========================================");

            // 显示Toast提示
            String message = getPageDisplayName(pageName) + " - 已接收筛选结果";
            if (data != null && !data.isEmpty()) {
                message += "（" + data.size() + "项）";
            }
            toast(message);

            // 这里可以根据不同的页面做不同的处理
            handleFilterResult(pageName, data);
        }
    }

    /**
     * 处理筛选结果
     * <p>
     * 根据不同的筛选页面，执行不同的业务逻辑
     *
     * @param pageName 页面名称
     * @param data     筛选数据
     */
    private void handleFilterResult(String pageName, Map<String, Object> data) {
        // 根据页面名称处理不同的筛选结果
        switch (pageName) {
            case "FilterSalespersonActivity":
                // 处理业务员筛选结果
                handleSalespersonFilter(data);
                break;

            case "FilterPromotionVisitActivity":
                // 处理促销拜访筛选结果
                handlePromotionVisitFilter(data);
                break;

            case "FilterVisitManagementActivity":
                // 处理拜访管理筛选结果
                handleVisitManagementFilter(data);
                break;

            case "FilterPaymentActivity":
                // 处理收款单筛选结果
                handlePaymentFilter(data);
                break;

            case "FilterWireTransferActivity":
                // 处理电汇收款筛选结果
                handleWireTransferFilterResult(data);
                break;

            case "FilterSalesOrderActivity":
                // 处理销售订单筛选结果
                handleSalesOrderFilterResult(data);
                break;

            case "FilterProxyPaymentActivity":
                // 处理代支付筛选结果
                handleProxyPaymentFilterResult(data);
                break;

            default:
                // 其他页面暂不处理
                LogUtils.d(TAG, "暂未处理该页面的筛选结果: " + pageName);
                break;
        }
    }

    /**
     * 处理业务员筛选结果
     */
    private void handleSalespersonFilter(Map<String, Object> data) {
        // 示例：获取筛选数据
        String salesArea = (String) data.get("sales_area");
        String department = (String) data.get("department");
        String salesperson = (String) data.get("salesperson");

        LogUtils.d(TAG, "业务员筛选 - 销售区域: " + salesArea);
        LogUtils.d(TAG, "业务员筛选 - 部门: " + department);
        LogUtils.d(TAG, "业务员筛选 - 业务员: " + salesperson);

        // TODO: 这里可以调用API请求数据
        // requestSalespersonData(salesArea, department, salesperson);
    }

    /**
     * 处理促销拜访筛选结果
     */
    private void handlePromotionVisitFilter(Map<String, Object> data) {
        // 示例：获取筛选数据
        String salesArea = (String) data.get("sales_area");
        String visitTime = (String) data.get("visit_time");
        String dateStart = (String) data.get("date_range_start");
        String dateEnd = (String) data.get("date_range_end");

        LogUtils.d(TAG, "促销拜访筛选 - 销售区域: " + salesArea);
        LogUtils.d(TAG, "促销拜访筛选 - 拜访时间: " + visitTime);
        LogUtils.d(TAG, "促销拜访筛选 - 开始日期: " + dateStart);
        LogUtils.d(TAG, "促销拜访筛选 - 结束日期: " + dateEnd);

        // TODO: 这里可以调用API请求数据
        // requestPromotionVisitData(salesArea, visitTime, dateStart, dateEnd);
    }

    /**
     * 处理拜访管理筛选结果
     */
    private void handleVisitManagementFilter(Map<String, Object> data) {
        // 示例：获取筛选数据
        String salesArea = (String) data.get("sales_area");
        String visitType = (String) data.get("visit_type");
        String visitStatus = (String) data.get("visit_status");

        LogUtils.d(TAG, "拜访管理筛选 - 销售区域: " + salesArea);
        LogUtils.d(TAG, "拜访管理筛选 - 拜访类型: " + visitType);
        LogUtils.d(TAG, "拜访管理筛选 - 拜访状态: " + visitStatus);

        // TODO: 这里可以调用API请求数据
        // requestVisitManagementData(salesArea, visitType, visitStatus);
    }

    /**
     * 处理收款单筛选结果
     */
    private void handlePaymentFilter(Map<String, Object> data) {
        // 示例：获取筛选数据
        String salesArea = (String) data.get("sales_area");
        String submitTime = (String) data.get("submit_time");
        String dateStart = (String) data.get("date_range_start");
        String dateEnd = (String) data.get("date_range_end");
        String handler = (String) data.get("handler");
        String paymentMethod = (String) data.get("payment_method");
        String amountMin = (String) data.get("amount_min");
        String amountMax = (String) data.get("amount_max");
        String submitter = (String) data.get("submitter");
        String documentType = (String) data.get("document_type");
        String paymentType = (String) data.get("payment_type");

        LogUtils.d(TAG, "收款单筛选 - 销售区域: " + salesArea);
        LogUtils.d(TAG, "收款单筛选 - 提交时间: " + submitTime);
        LogUtils.d(TAG, "收款单筛选 - 开始日期: " + dateStart);
        LogUtils.d(TAG, "收款单筛选 - 结束日期: " + dateEnd);
        LogUtils.d(TAG, "收款单筛选 - 经办人: " + handler);
        LogUtils.d(TAG, "收款单筛选 - 收款方式: " + paymentMethod);
        LogUtils.d(TAG, "收款单筛选 - 最小金额: " + amountMin);
        LogUtils.d(TAG, "收款单筛选 - 最大金额: " + amountMax);
        LogUtils.d(TAG, "收款单筛选 - 提交人: " + submitter);
        LogUtils.d(TAG, "收款单筛选 - 单据类型: " + documentType);
        LogUtils.d(TAG, "收款单筛选 - 付款类型: " + paymentType);

        // TODO: 这里可以调用API请求数据
        // requestPaymentData(salesArea, submitTime, dateStart, dateEnd, handler, paymentMethod, amountMin, amountMax, submitter, documentType, paymentType);
    }

    /**
     * 根据Activity类名获取显示名称
     */
    private String getPageDisplayName(String pageName) {
        switch (pageName) {
            case "FilterSalespersonActivity":
                return "业务员筛选";
            case "FilterPromotionVisitActivity":
                return "促销拜访筛选";
            case "FilterVisitManagementActivity":
                return "拜访管理筛选";
            case "FilterFreezerManagementActivity":
                return "冰柜管理筛选";
            case "FilterFreezerStatisticsActivity":
                return "冰柜统计筛选";
            case "FilterMyApprovalActivity":
                return "我的审批筛选";
            case "FilterMyApplicationActivity":
                return "我的申请筛选";
            case "FilterClockInRecordActivity":
                return "打卡记录筛选";
            case "FilterTerminalManagementActivity":
                return "终端管理筛选";
            case "FilterTerminalSalesAnalysisActivity":
                return "终端销售分析筛选";
            case "FilterDistributorActivity":
                return "配送商筛选";
            case "FilterGoldBeanManagementActivity":
                return "金豆管理筛选";
            case "FilterSalesLeadActivity":
                return "销售线索筛选";
            case "FilterPaymentActivity":
                return "收款单筛选";
            case "FilterWireTransferActivity":
                return "电汇收款筛选";
            case "FilterSalesOrderActivity":
                return "销售订单筛选";
            case "FilterProxyPaymentActivity":
                return "代支付筛选";
            default:
                return pageName;
        }
    }

    /**
     * 处理电汇收款筛选结果
     */
    private void handleWireTransferFilterResult(Map<String, Object> data) {
        LogUtils.d(TAG, "========== 电汇收款筛选结果 ==========");

        // 获取筛选数据
        String status = (String) data.get("status");
        String salesArea = (String) data.get("sales_area");
        String submitTime = (String) data.get("submit_time");
        String dateStart = (String) data.get("date_start");
        String dateEnd = (String) data.get("date_end");
        String handler = (String) data.get("handler");
        String amountMin = (String) data.get("amount_min");
        String amountMax = (String) data.get("amount_max");
        String submitter = (String) data.get("submitter");

        LogUtils.d(TAG, "电汇收款筛选 - 状态: " + status);
        LogUtils.d(TAG, "电汇收款筛选 - 销售区域: " + salesArea);
        LogUtils.d(TAG, "电汇收款筛选 - 提交时间: " + submitTime);
        LogUtils.d(TAG, "电汇收款筛选 - 开始日期: " + dateStart);
        LogUtils.d(TAG, "电汇收款筛选 - 结束日期: " + dateEnd);
        LogUtils.d(TAG, "电汇收款筛选 - 经办人: " + handler);
        LogUtils.d(TAG, "电汇收款筛选 - 最小金额: " + amountMin);
        LogUtils.d(TAG, "电汇收款筛选 - 最大金额: " + amountMax);
        LogUtils.d(TAG, "电汇收款筛选 - 提交人: " + submitter);

        // TODO: 这里可以调用API请求数据
        // requestWireTransferData(status, salesArea, submitTime, dateStart, dateEnd, handler, amountMin, amountMax, submitter);
    }

    /**
     * 处理销售订单筛选结果
     */
    private void handleSalesOrderFilterResult(Map<String, Object> data) {
        LogUtils.d(TAG, "========== 销售订单筛选结果 ==========");

        // 获取筛选数据
        String orderStatus = (String) data.get("order_status");
        String terminal = (String) data.get("terminal");
        String distributor = (String) data.get("distributor");
        String salesperson = (String) data.get("salesperson");
        String submitter = (String) data.get("submitter");
        String salesArea = (String) data.get("sales_area");

        LogUtils.d(TAG, "销售订单筛选 - 订单状态: " + orderStatus);
        LogUtils.d(TAG, "销售订单筛选 - 终端: " + terminal);
        LogUtils.d(TAG, "销售订单筛选 - 配送商: " + distributor);
        LogUtils.d(TAG, "销售订单筛选 - 业务员: " + salesperson);
        LogUtils.d(TAG, "销售订单筛选 - 提交人: " + submitter);
        LogUtils.d(TAG, "销售订单筛选 - 销售区域: " + salesArea);

        // TODO: 这里可以调用API请求数据
        // requestSalesOrderData(orderStatus, terminal, distributor, salesperson, submitter, salesArea);
    }

    /**
     * 处理代支付筛选结果
     */
    private void handleProxyPaymentFilterResult(Map<String, Object> data) {
        LogUtils.d(TAG, "========== 代支付筛选结果 ==========");

        // 获取筛选数据
        String salesperson = (String) data.get("salesperson");
        String distributor = (String) data.get("distributor");
        String salesArea = (String) data.get("sales_area");

        LogUtils.d(TAG, "代支付筛选 - 业务员: " + salesperson);
        LogUtils.d(TAG, "代支付筛选 - 配送商: " + distributor);
        LogUtils.d(TAG, "代支付筛选 - 销售区域: " + salesArea);

        // TODO: 这里可以调用API请求数据
        // requestProxyPaymentData(salesperson, distributor, salesArea);
    }
}
